package GUI;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;

import Controller.Client;

import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;

import Entity.OneTimeCostumerOrder;
import Entity.OneTimeCostumerOrder.oneTimeCostumerEnum;

import java.awt.Choice;
import java.awt.Label;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * this is the GUI for the information station one time order. the customer can enter his id, car number, email, leaving and arriving hour. 
 * @author Or Marx
 *
 */
public class InformationStationOneTimeCostumerOrderGui extends JPanel{
	JFrame frame;
	JPanel lastPanel;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	public void setLastPanel (JPanel panel)
	{
		this.lastPanel=panel;
	}
	public void setFrame (JFrame frame)
	{
		this.frame=frame;
	}
	/**
	 * is the constructor. it has the confirm button and it will validate the inserted data.
	 */
	public InformationStationOneTimeCostumerOrderGui() {
		setLayout(null);
		
		JLabel lblEnterYourDetails = new JLabel("enter your details for one time entering");
		lblEnterYourDetails.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblEnterYourDetails.setBounds(317, 47, 384, 40);
		add(lblEnterYourDetails);
		
		
		double price=Client.getCurrentPrices(Client.getParkingLot()).getOccasionalParkingPlace();
		JLabel lblPriceXx = new JLabel("price: "+price+" $ per hour");
		lblPriceXx.setBounds(415, 99, 115, 14);
		add(lblPriceXx);
		
		final Choice choice = new Choice();
		choice.setBounds(391, 234, 47, 14);
		add(choice);
		
		final Choice choice_1 = new Choice();
		choice_1.setBounds(443, 234, 46, 20);
		add(choice_1);
		
		final Choice choice_2 = new Choice();
		choice_2.setBounds(495, 234, 46, 14);
		add(choice_2);
		
		JLabel lblLeavingDate = new JLabel("leaving date");
		lblLeavingDate.setBounds(272, 240, 73, 14);
		add(lblLeavingDate);
		
		final Choice choice_3 = new Choice();
		choice_3.setBounds(392, 269, 46, 23);
		add(choice_3);
		
		final Choice choice_4 = new Choice();
		choice_4.setBounds(450, 269, 38, 20);
		add(choice_4);
		
		JButton btnCancel = new JButton("cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				frame.setContentPane((JPanel)lastPanel);
			}
		});
		btnCancel.setBounds(579, 345, 89, 23);
		add(btnCancel);
		
		JButton btnContinue = new JButton("continue");
		btnContinue.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				if(isValidDate(choice_1.getSelectedItem() + "/" + choice_2.getSelectedItem() + "/" + choice.getSelectedItem()))
				{
				Date d1=new Date(Integer.valueOf(choice.getSelectedItem())-1900,Integer.valueOf(choice_1.getSelectedItem()),Integer.valueOf(choice_2.getSelectedItem()),Integer.valueOf(choice_3.getSelectedItem()),Integer.valueOf(choice_4.getSelectedItem()),0);
				OneTimeCostumerOrder order=new OneTimeCostumerOrder(textField.getText(), d1, textField_2.getText(), textField_1.getText());
				oneTimeCostumerEnum validation=order.validate();
				if (validation == oneTimeCostumerEnum.CORRECT)
					{
						if(Client.getParkingLots_sorted()[Client.getParkingLot()-1].addOrder(new Date(), d1)== true)
						{
							Client.getParkingLots_sorted()[Client.getParkingLot()-1].insertCar(order.getCar_number(), order.getLeaving_hour());
							order.saveToDB();
							JOptionPane.showMessageDialog(null,"enjoy your parking!","one time parking",JOptionPane.INFORMATION_MESSAGE);
							frame.setContentPane((JPanel)lastPanel);
						}
						else
							JOptionPane.showMessageDialog(null,"the parking lot is full or will be full before the entered leaving date.\nchoose sooner leaving date or come back later.","one time parking",JOptionPane.ERROR_MESSAGE);
					}
				else if (validation == oneTimeCostumerEnum.CAR_NUMBER_ERROR)
					JOptionPane.showMessageDialog(null,"illegal car number","one time parking",JOptionPane.ERROR_MESSAGE);

				else if (validation == oneTimeCostumerEnum.EMAIL_ERROR)
					JOptionPane.showMessageDialog(null,"illegal Email","one time parking",JOptionPane.ERROR_MESSAGE);

				else if (validation == oneTimeCostumerEnum.ID_ERROR)
					JOptionPane.showMessageDialog(null,"illegal ID number","one time parking",JOptionPane.ERROR_MESSAGE);

				}
				else
					JOptionPane.showMessageDialog(null,"illegal date","one time parking",JOptionPane.ERROR_MESSAGE);
			}
		});
		btnContinue.setBounds(253, 345, 89, 23);
		add(btnContinue);
		
		JLabel lblId = new JLabel("ID");
		lblId.setBounds(272, 137, 46, 14);
		add(lblId);
		
		JLabel lblCarNumber = new JLabel("car number");
		lblCarNumber.setBounds(272, 170, 73, 14);
		add(lblCarNumber);
		
		JLabel lblEmailAddress = new JLabel("email address");
		lblEmailAddress.setBounds(272, 205, 95, 14);
		add(lblEmailAddress);
		
		JLabel lblLeavingHour = new JLabel("leaving hour");
		lblLeavingHour.setBounds(272, 275, 95, 14);
		add(lblLeavingHour);
		
		textField = new JTextField();
		textField.setBounds(392, 134, 86, 20);
		add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(392, 167, 86, 20);
		add(textField_1);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setBounds(392, 202, 86, 20);
		add(textField_2);
		textField_2.setColumns(10);
		
		
		
		JLabel lblyymmdd = new JLabel("(YY.MM.DD)");
		lblyymmdd.setBounds(561, 240, 80, 14);
		add(lblyymmdd);
		
		JLabel lblhhmm = new JLabel("(HH.MM)");
		lblhhmm.setBounds(561, 275, 46, 14);
		add(lblhhmm);
		Date now=new Date();
		for (int i=0; i<10;i++)
		{
			choice.add(String.valueOf(1900+now.getYear()+i));
		}
		for (int i=1; i<=12;i++)
		{
			choice_1.add(String.valueOf(i));
		}
		for (int i=1; i<=31;i++)
		{
			choice_2.add(String.valueOf(i));
		}
		for (int i=0; i<=23;i++)
		{
			choice_3.add(String.valueOf(i));
		}
		for (int i=0; i<=3;i++)
		{
			choice_4.add(String.valueOf(i*15));
		}
	}
	private static boolean isValidDate(String input) {
        String formatString = "MM/dd/yyyy";

        try {
            SimpleDateFormat format = new SimpleDateFormat(formatString);
            format.setLenient(false);
            format.parse(input);
        } catch (ParseException e) {
            return false;
        } catch (IllegalArgumentException e) {
            return false;
        }

        return true;
    }
	
}
